Yoast SEO is a great plugin for optimizing your site for SEO. It is important that your articles are providing the right information to social media when being shared. Facebook uses Open Graph meta tags to show the correct information. In this tutorial, you’ll learn how to change the Yoast SEO Open Graph Meta through code.
The team behind Yoast SEO did a great job using WordPress Plugin API to make their plugin extensible. If you want to learn a bit about that, I have written an article on 5 Ways to Make your WordPress Plugin Really Extensible.
This plugin uses the class WPSEO_OpenGraph
to add the Yoast SEO Open Graph meta tags. When changing the Open Graph meta, you should hook into wpseo_opengraph
.
Locale
This can be used to assign a different language if you want it. Maybe you’re using a different locale on your site by default but want to change the locale for a particular article? Let’s see how we can do that now:
We are checking for the ID of the article we want to change the language. Since this article is assumed to be written in Croatian, we will set it to be a valid Facebook tag for Croatian Language.
Type
The type will tell Facebook that the URL that we are sharing is about an eBook or any type of a book.
Here we are again checking againts an ID. Since that ID is actually an eBook landing page, we will tell to Facebook that we are sharing information about an eBook.
For all types, please visit this page: https://developers.facebook.com/docs/reference/opengraph#object-type.
Title
Title is the one of the most important tags. This can change how people will react to your shared content. Let’s say we have a product that was already sold to more than 100 people. We want to tell that in a title!
Pretty awesome, right?
Description
This is another Yoast SEO Open Graph meta that we should use to get attention from people on Social Media. We are already using it when we create a custom meta description for our site. But what if we want to have a different description on social media?
URL
You can also change the URL. I assume this is something you won’t change that much, but you could change the URL. Just be careful that the URL you are putting here is having the same meta tags defined before. Otherwise, Facebook will pull some different meta data from that URL.
For this example, I did not change the URL completely. I have just added another query parameter to it.
Images
This is probably the most interesting part since we are all very visual. That is why many sites are creating specific images just for social media to get our attention.
Yoast SEO Open Graph image meta allows us to put more than one image.
If you look closely, I am not using the add_filter
but instead add_action
. That is because, Yoast SEO allowed us to add other images. We can ofcouse, filter some of the images that are being added through options. That can be done like this:
Other Yoast SEO Open Graph Meta Tags
There are some other tags that we could change:
wpseo_opengraph_admin
– Site Owner (if App ID is not present)wpseo_opengraph_site_name
– Site Namewpseo_opengraph_author_facebook
– Facebook Profile URLwpseo_opengraph_show_publish_date
– True/False to show the publish date
Dynamic Open Graph Tags
Yoast SEO gives us another, deeper, layer where we can hook into and change other data. Some of the present filters here are the hardcoded ones that we can use. But, we can also change them differently and get access to other tags that are set without the filters.
That is done by the method og_tag
:
Be sure to check other methods to see how you can change them. Here are some of the examples for filters you can use:
wpseo_og_article:publisher
wpseo_og_fb:app_id
wpseo_og_article:tag
Be sure to check other tags and methods here: https://github.com/Yoast/wordpress-seo/blob/trunk/frontend/class-opengraph.php
Have you ever changed the Yoast SEO Meta tags yourself? Or if you have even worked with the Open Graph, please do share your experience in the comments below.
Become a Sponsor
Hi Igor,
very interesting argument,
I’m looking for a solution in order to dinamicaly change the url path from ‘/%category%/%postname%-%post_id%.html’ to ‘/%category%/%postname%/’.
Do you think this may be possible?
Manuela
Hi Manuela, I have not tried that. In your case, the previous path would still exist?
Thank you for this. It was all I needed.
Does this implementation still work with the newer versions of Yoast.
eg Version 7.2 ?
As of current Yoast this didn’t work for me. I created my own plugin with:
<?php
function change_title($title){
return 'TEST';
}
add_filter( 'wpseo_opengraph_title', 'change_title' );
Hi JJ, I’ll try the code in the tutorial against the newest Yoast version and update it where possible.
Not working here. Just tried to change the image.
Can you help me bro?
Hi Andrew, how does it now work? The action approach or the filter approach?
Great, Thanks for sharing. Do you think one could pull an ACF field for the custom og description?
It should be possible.